package cn.ekuma.epos.datalogic.define.dao.crm.contract;

import cn.ekuma.data.dao.ModifiedLogDAO;
import cn.ekuma.epos.datalogic.define.dao.ProductDAO;
import cn.ekuma.epos.db.table.crm.contract.I_ContractProduct;

import com.openbravo.bean.Customer;
import com.openbravo.bean.Product;
import com.openbravo.bean.crm.contract.ContractProduct;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

public class ContractProductDAO extends ModifiedLogDAO<ContractProduct>{
	ProductDAO productDAO;
	
	public ContractProductDAO(I_Session s,ProductDAO productDAO) {
		super(s);
		this.productDAO=productDAO;
	}

	@Override
	public TableDefinition getTable() {
		return new TableDefinition(s,
                "CONTRACT_PRODUCT",
                new Field[]{
                     new Field(I_ContractProduct.ID,Datas.STRING,Formats.STRING),
                     new Field(I_ContractProduct.CUSTOMER_ID,Datas.STRING,Formats.STRING,Customer.class),
                     new Field(I_ContractProduct.PRODUCT_ID,Datas.STRING,Formats.STRING,Product.class),
                     new Field(I_ContractProduct.CONTRACTPRICE,Datas.DOUBLE,Formats.DOUBLE),
                     new Field(I_ContractProduct.MAXPRICE,Datas.DOUBLE,Formats.DOUBLE),
                     new Field(I_ContractProduct.MINPRICE,Datas.DOUBLE,Formats.DOUBLE),
                     new Field(I_ContractProduct.PRODUCTSTATE,Datas.INT,Formats.INT),
                     new Field(I_ContractProduct.DISTRIBUTIONMODE,Datas.INT,Formats.INT),
                     new Field(I_ContractProduct.VALIDITYDATE,Datas.TIMESTAMP,Formats.TIMESTAMP),
                     new Field(I_ContractProduct.LASTMODIFIED,Datas.TIMESTAMP,Formats.TIMESTAMP)
                    },
                new int[]{0});
	}

	@Override
	public void writeInsertValues(DataWrite dp, ContractProduct obj)
			throws BasicException {
	      dp.setString(1,obj.getId());
	      dp.setString(2,obj.getCustomerId());
	      dp.setString(3,obj.getProductId());
	      dp.setDouble(4,obj.getContractPrice());
	      dp.setDouble(5,obj.getMaxPrice());
	      dp.setDouble(6,obj.getMinPrice());
	      dp.setInt(7,obj.getState());
	      dp.setInt(8,obj.getDistributionMode());
	      dp.setTimestamp(9,obj.getContractValidityDate());
	      dp.setTimestamp(10,obj.getLastModified());
	}

	@Override
	public Class getSuportClass() {
		return ContractProduct.class;
	}

	@Override
	public ContractProduct readValues(DataRead dr, ContractProduct obj)
			throws BasicException {
		if(obj==null)
			obj=new ContractProduct();
		obj.setId(dr.getString(1));
		obj.setCustomerId(dr.getString(2));
		obj.setProductId(dr.getString(3));
		obj.setContractPrice(dr.getDouble(4));
		obj.setMaxPrice(dr.getDouble(5));
		obj.setMinPrice(dr.getDouble(6));
		obj.setState(dr.getInt(7));
		obj.setDistributionMode(dr.getInt(8));
		obj.setContractValidityDate(dr.getTimestamp(9));
		obj.setLastModified(dr.getTimestamp(10));
		
		obj.setIncludeProduct(productDAO.getProductInfo(obj.getProductId()));
		return obj;
	}

	
}
